package com.gasolinator.a.app.activity.map;

import android.content.Context;
import android.graphics.*;
import android.graphics.drawable.Drawable;
import com.gasolinator.a.app.GasolinatorStation;
import com.gasolinator.a.app.GasolinatorTypes;
import com.gasolinator.a.app.helper.NumberFormats;
import com.gasolinator.a.augmented_reality.data.ARData;
import com.gasolinator.a.augmented_reality.data.GasolinatorDataSource;
import com.gasolinator.a.augmented_reality.ui.GasolinatorMarker;
import com.gasolinator.a.augmented_reality.ui.Marker;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

import java.util.List;

public class GasolinatorOverlay extends Overlay
{
    GasolinatorMapActivity activity;
    GasolinatorTypes types;
    private List<Marker> gasolinatorMarkers;
    private GasolinatorStation gasolinatorStation;


    public GasolinatorOverlay(GasolinatorMapActivity mapActivity) {
        GasolinatorDataSource gasolinator = GasolinatorDataSource.getInstance(mapActivity);
        gasolinatorMarkers = gasolinator.getGasolinatorMarkers();
        this.activity = mapActivity;
        this.types = new GasolinatorTypes(mapActivity);
    }

    private int dipToPixels(Context paramContext, float paramFloat)
    {
        return (int)(paramContext.getResources().getDisplayMetrics().density * paramFloat + 0.5F);
    }

    private void drawMarker(int stationId, Point paramPoint, Canvas paramCanvas)
    {
        GasolinatorTypes gasolinatorTypes = types;
        byte b = getStation(stationId).getType();
        Drawable localDrawable = gasolinatorTypes.getMarker(b);
        Paint localPaint = new Paint();
        localPaint.setColor(-16777216);
        float f1 = paramPoint.x;
        int i = paramPoint.y;
        int j = dipToPixels(activity, 46.0F);
        float f2 = i - j;
        float f3 = paramPoint.x;
        float f4 = paramPoint.y;
        paramCanvas.drawLine(f1, f2, f3, f4, localPaint);
        int k = paramPoint.x;
        int m = paramPoint.y;
        int n = dipToPixels(activity, 48.0F);
        int i1 = m - n;
        int i2 = paramPoint.x;
        int i3 = dipToPixels(activity, 32.0F);
        int i4 = i2 + i3;
        int i5 = paramPoint.y;
        int i6 = dipToPixels(activity, 16.0F);
        int i7 = i5 - i6;
        localDrawable.setBounds(k, i1, i4, i7);
        localDrawable.draw(paramCanvas);
    }

    private GasolinatorStation getStation(int stationId) {
        return ((GasolinatorMarker)gasolinatorMarkers.get(stationId)).getStation();
    }

    private void drawText(int paramInt, Point paramPoint, Canvas paramCanvas, boolean paramBoolean)
    {
        Paint localPaint = new Paint();
        localPaint.setFlags(129);
        Typeface localTypeface1 = Typeface.DEFAULT_BOLD;
        Typeface localTypeface2 = localPaint.setTypeface(localTypeface1);
        double d;
        if (paramBoolean)
        {
            float f1 = dipToPixels(activity, 15.0F);
            localPaint.setTextSize(f1);
            d = getStation(paramInt).getPrice("gasoil");

            if (d != -1.0D)
                for (String str = "?"; ; str = NumberFormats.priceFormat(Double.valueOf(d)))
                {
                    Rect localRect = new Rect();
                    int i = str.length();
                    localPaint.getTextBounds(str, 0, i, localRect);
                    localPaint.setColor(-1);
                    int j = paramPoint.x;
                    int k = dipToPixels(activity, 5.0F);
                    float f2 = j + k;
                    int m = paramPoint.y;
                    int n = localRect.bottom;
                    int i1 = localRect.top;
                    int i2 = n - i1;
                    float f3 = m - i2;
                    int i3 = paramPoint.x;
                    int i4 = dipToPixels(activity, 5.0F);
                    int i5 = i3 + i4;
                    int i6 = localRect.right;
                    int i7 = localRect.left;
                    int i8 = i6 - i7;
                    float f4 = i5 + i8;
                    float f5 = paramPoint.y + 1;
                    paramCanvas.drawRect(f2, f3, f4, f5, localPaint);
                    localPaint.setColor(-16777216);
                    int i9 = paramPoint.x;
                    int i10 = dipToPixels(activity, 5.0F);
                    float f6 = i9 + i10;
                    float f7 = paramPoint.y;
                    paramCanvas.drawText(str, f6, f7, localPaint);
                    float f8 = dipToPixels(activity, 12.0F);
                    localPaint.setTextSize(f8);
                    break;
                }
        }
    }

    public void draw(Canvas paramCanvas, MapView paramMapView, boolean paramBoolean)
    {
        Projection localProjection = paramMapView.getProjection();
        int i = 0;
        int k = 0;
        if ((!paramBoolean) && (ARData.getCurrentLocation() != null))
        {
            i = 0;
            int j = gasolinatorMarkers.size();
            if (i < j) //break label85;
                k = 0;
        }
        GeoPoint localGeoPoint1 = null;



//        if (k >= gasolinatorMarkers.size())
//        {
//            if (this.gasolinatorChopperData.getSelectedPointIndex() == -1) return;
//            localGeoPoint1 = this.gasolinatorChopperData.getSelectedPoint();
//            if (localGeoPoint1 != null) {
//                //break label353;
//                return;
//            }
//            if (i != this.gasolinatorChopperData.getSelectedPointIndex())
//            {
//                GeoPoint localGeoPoint2 = this.gasolinatorChopperData.getPointToDraw(i);
//                Point localPoint1 = new Point();
//                Point localPoint2 = localProjection.toPixels(localGeoPoint2, localPoint1);
//                if (localPoint1.x > 65486)
//                {
//                    int i1 = localPoint1.x;
//                    int i2 = paramCanvas.getWidth() + 50;
//                    if ((i1 < i2) && (localPoint1.y > 65486))
//                    {
//                        int i3 = localPoint1.y;
//                        int i4 = paramCanvas.getHeight() + 50;
//                        if (i3 < i4)
//                            drawMarker(i, localPoint1, paramCanvas);
//                    }
//                }
//            }
//            i += 1;
//
//
//
//            int i5 = this.gasolinatorChopperData.getSelectedPointIndex();
//            if (k != i5)
//            {
//                GeoPoint localGeoPoint3 = this.gasolinatorChopperData.getPointToDraw(k);
//                Point localPoint3 = new Point();
//                Point localPoint4 = localProjection.toPixels(localGeoPoint3, localPoint3);
//                if (localPoint3.x > 65486)
//                {
//                    int i6 = localPoint3.x;
//                    int i7 = paramCanvas.getWidth() + 50;
//                    if ((i6 < i7) && (localPoint3.y > 65486))
//                    {
//                        int i8 = localPoint3.y;
//                        int i9 = paramCanvas.getHeight() + 50;
//                        if (i8 < i9) drawText(k, localPoint3, paramCanvas, false);
//                    }
//                }
//            }
//            k += 1;
//        }
//        //label353:
//        Point localPoint5 = new Point();
//        Point localPoint6 = localProjection.toPixels(localGeoPoint1, localPoint5);
//        if (localPoint5.x > 65486)
//        {
//            int i10 = localPoint5.x;
//            int i11 = paramCanvas.getWidth() + 50;
//            if ((i10 < i11) && (localPoint5.y > 65486))
//            {
//                int i12 = localPoint5.y;
//                int i13 = paramCanvas.getHeight() + 50;
//                if (i12 < i13)
//                {
//                    int i14 = this.gasolinatorChopperData.getSelectedPointIndex();
//                    drawMarker(i14, localPoint5, paramCanvas);
//                }
//            }
//        }
//        int i15 = this.gasolinatorChopperData.getSelectedPointIndex();
//        drawText(i15, localPoint5, paramCanvas, true);

        super.draw(paramCanvas, paramMapView, paramBoolean);
    }

    public boolean onTap(GeoPoint paramGeoPoint, MapView paramMapView)
    {
//        int i = this.gasolinatorChopperData.getNearestIndex(paramGeoPoint);
//        int j = this.gasolinatorChopperData.getSelectedPointIndex();
//        int k;
//        if (i != j)
//        {
//            Toast localToast = Toast.makeText(this.activity, 2131165202, 0);
//            localToast.setGravity(48, 0, 0);
//            localToast.show();
//            this.gasolinatorChopperData.setSelectedPointIndex(i);
//            if (this.gasolinatorChopperData.getSelectedPointIndex() != -1)
//            {
//                GeoPoint localGeoPoint = this.gasolinatorChopperData.getSelectedPoint();
//                paramMapView.getController().animateTo(localGeoPoint);
//            }
//            if (this.gasolinatorChopperData.getSelectedPointIndex() != -1){
//                k = 0;
//            }
//        }
//        while (true)
//        {
//
//            GasolinatorMapActivity localGasolinatorStationMapActivity = this.activity;
//            Intent localIntent1 = new Intent(localGasolinatorStationMapActivity, GasolinatorStationDetailActivity.class);
//            Bundle localBundle = new Bundle();
//            localBundle.putInt("selectedGasStation", i);
//            localBundle.putBoolean("calledFromMap", true);
//            Intent localIntent2 = localIntent1.putExtras(localBundle);
//            this.activity.startActivity(localIntent1);
//            k = 1;
//        }
        return true;
    }
}